package cn.yogehaoren.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @author WangNing
 * @since 1.0
 */
public class MyACKConsumer extends DefaultConsumer {
    private Channel channel;
    private int zeroTime = 0;
    public MyACKConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }


    /**
     *
     * @param consumerTag 消费标签
     * @param envelope 环境变量
     * @param properties properties
     * @param body 消息内容
     * @throws IOException 可能抛出的异常
     */
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

        System.out.println("------------- consumer message ---------------");
        System.out.println("body: " + new String(body));
        if((int)properties.getHeaders().get("num")==0&&zeroTime<10){
            zeroTime += 1;
            channel.basicNack(envelope.getDeliveryTag(), false, true);

        }else {
            channel.basicAck(envelope.getDeliveryTag(), false);
        }

    }


}
